<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2020/06/24 10:16
 * Desc: 积分相关功能--必须要进行登录检查
 */
class Controller_Pc_Api_Standard_Credit extends Controller_Pc_Api_Base
{

    private $_mid;

    public function before()
    {
        parent::before();
        // 检查用户是否有效
        $mid = intval($this->request_body->content->mid);
        $check_member = Model_Api_Standard_Pub::check_member_effective($mid);
        if($check_member['status'] == false)
        {
            $this->send_datagrams($this->client_info['id'], $check_member, $this->client_info['secret_key']);
        }

        $this->_mid = $mid;
    }


    // 获取会员积分数据
    public function action_get_user_credit()
    {
        // 获取用户信息
        $memberInfo = Model_Member::get_member_info($this->_mid);
        $creditNum = $memberInfo['jifen'] ? $memberInfo['jifen'] : 0;
        $result = array('status'=> true, 'msg'=> '获取成功','creditNum'=> $creditNum);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    // 获取会员积分记录
    public function action_get_user_credit_list()
    {
        $page = intval($this->request_body->content->page);
        $pagesize = intval($this->request_body->content->pagesize);
        $type = intval($this->request_body->content->type);

        $log_arr = Model_Member_Jifen_Log::log_list($this->_mid,$page,$pagesize,$type);
        $arr = $log_arr['list'];

        // 处理返回的积分列表数据
        foreach($arr as &$ar)
        {
            $ar['addtime'] = date('Y-m-d H:i',$ar['addtime']);
            $ar['point'] = $ar['type'] == 1 ? '-'.$ar['jifen'] : '+'.$ar['jifen'];
        }

        // 判断是否获取全部
        $totalNum  = $log_arr['total'];
        $nowNum    = $pagesize * $page; // 当前获取总条数
        $completed = false;
        if($totalNum <= $nowNum)
        {
            $completed = true;
        }

        $result = array('status'=> true, 'msg'=> '获取成功', 'list'=> $arr, 'completed'=> $completed);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    // 检查积分是否开启 并返回用户积分数据
    public function action_get_jifen_price_Info()
    {
        $jifen = array();
        // 检查产品是否开启积分
        $jifenid = intval($this->request_body->content->jifenid);
        $typeid = intval($this->request_body->content->typeid);
        // 积分配置
        $jifentprice_info = Model_Jifen_Price::get_used_jifentprice($jifenid, $typeid);
        $jifen['info'] = $jifentprice_info;
        // 用户积分
        $userInfo = Model_Member::get_member_byid($this->_mid);
        $jifen['userjifen'] = $userInfo['jifen'];
        // 返回数据
        $result = array('status'=> true, 'msg'=> '获取成功', 'jifen'=> $jifen);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

}